package ar.com.photo_admin.domain

import java.util.List;
import java.util.Set;

class EventAlbum extends DomainObject {

    String name
	
	String description

	Event event
	
	List photoInAlbums
	
	Set userAlbumAccesses
	
	EventPhoto coverPhoto

	static belongsTo = [Event]
		
	static hasMany = [photoInAlbums : PhotoInAlbum, userAlbumAccesses : UserAlbumAccess]

	static constraints = {
		name(blank:false)
		description(nullable:true,blank:true)
		event(nullable:false)
		coverPhoto(nullable:true)
		photoInAlbums(nullable:true)
	}
	
	static mapping = {
		coverPhoto column: 'cover_photo__id'
	}	
	
	List getAlbumPhotos() {
		return photoInAlbums.collect { it.photo }
	}		
	
	def removeAllPhotos() {
		List photos = getAlbumPhotos()
		
		photos.each { photo ->
			PhotoInAlbum.unlink(photo, this)
		}
	}
	
	List addToAlbumPhotos(EventPhoto photo) {
		PhotoInAlbum.link(photo, this)
		return getAlbumPhotos()
	}
	
	List removeFromAlbumPhotos(EventPhoto photo) {
		PhotoInAlbum.unlink(photo, this)
		return getAlbumPhotos()
	}
	
	Set getUsersWhoCanAccess() {
		return userAlbumAccesses.collect{ it.user }
	}
	
	EventAlbum addToUserAlbumAccesses(User user) {
		UserAlbumAccess.link(user, this)
		return this
	}
	
	EventAlbum removeFromUserAlbumAccesses(User user) {
		UserAlbumAccess.unlink(User, this)
		return this
	}
}
